﻿// 
//  MVVM-WPF-SL-WinStor Markup Binding Extensions
//  Copyright © 2013-2014 Alexander Paskhin /paskhin@hotmail.co.uk/ All rights reserved.
// 
//
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using WinRtMvvmBindingPack;
using Windows.UI.Xaml;

namespace UniversalWindowsAppW10.ViewModels
{
    [ViewModelClassAlias("MainPage")]
    public class MainWindowVm : INotifyPropertyChanged
    {
        #region Implementation of INotifyPropertyChanged interface
        /// <summary>
        /// Implementation of INotifyPropertyChanged interface
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;
        // This method is called by the Set accessor of each property.
        // The CallerMemberName attribute that is applied to the optional propertyName
        // parameter causes the property name of the caller to be substituted as an argument.
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion  Implementation of INotifyPropertyChanged interface

        /// <summary>
        /// Constructor
        /// </summary>
        public MainWindowVm()
        {

            _commandProxyEmpty = new CommandHandlerProxy(a => { }, a => false);
            _commandProxyInstance = new CommandHandlerProxy(ExecuteMethod, CanExecuteMethod);
            _commandProxy = _commandProxyInstance;
            ButtonExecuteProperty = ExecuteMethod;
            CanExecuteProperty = CanExecuteMethod;
            ButtonClickPropDelegate = ButtonClickProp;
        }

        public void View_Loaded(object sender, RoutedEventArgs e)
        {
            //// Perform actions when view is loaded

        }

        public void App_Exit(object sender, EventArgs e)
        {
            //// Perform actions when view is loaded
        }

        public void View_UnLoaded(object sender, RoutedEventArgs e)
        {
            // Perform actions when view is unloaded
        }

        /// <summary>
        /// Demo/test methods/ event handlers invocation counter.
        /// </summary>
        int _callCount;
        public int CallCounter
        {
            get { return _callCount++; }
        }

        // Legacy proper handling of Button Command property enable/ disable features.
        readonly CommandHandlerProxy _commandProxyInstance;
        readonly CommandHandlerProxy _commandProxyEmpty;
        CommandHandlerProxy _commandProxy;
        public CommandHandlerProxy CommandProxy
        {
            get
            { return _commandProxy; }
            set
            { _commandProxy = value; NotifyPropertyChanged(); }
        }

        /// <summary>
        /// The "can execute" Boolean flag.
        /// </summary>
        bool _canExecuteFlag = true;
        public bool CanExecuteFlag
        {
            get { return _canExecuteFlag; }
            set
            {
                _canExecuteFlag = value;
                NotifyPropertyChanged();

                // Legacy proper handling of Button Command property enable/ disable features.
                CommandProxy = value ? _commandProxyInstance : _commandProxyEmpty;

                if (ActionNotifyCanExecuteChanged != null) { ActionNotifyCanExecuteChanged(); }
                if (EventHandlerNotifyCanExecuteChanged != null) { EventHandlerNotifyCanExecuteChanged(this, EventArgs.Empty); }
                if (_propertyDelegateNotifyCanExecuteChanged != null) { _propertyDelegateNotifyCanExecuteChanged(); }
            }
        }

        /// <summary>
        /// Label message for Button Click Method
        /// </summary>
        String _buttonClickMsg = "MSG";
        public String ButtonClickMsg
        {
            get { return _buttonClickMsg; }
            set { _buttonClickMsg = value; NotifyPropertyChanged(); }
        }



        /// <summary>
        /// Event Handler for Button Click
        /// </summary>
        public void ButtonClickMethod(object sender, RoutedEventArgs e)
        {
            ButtonClickMsg = "ButtonClickMethod Called - Counter # " + CallCounter;
        }

        /// <summary>
        /// Event Handler via delegate type property for Button Click
        /// </summary>
        private RoutedEventHandler _buttonClickPropDelegate;
        public RoutedEventHandler ButtonClickPropDelegate
        {
            get { return _buttonClickPropDelegate; }
            set { _buttonClickPropDelegate = value; NotifyPropertyChanged(); }
        }
        private void ButtonClickProp(object sender, RoutedEventArgs e)
        {
            ButtonClickMsg = "ButtonClickProp Called     - Counter # " + CallCounter;
        }

        public Action<object> ButtonExecuteProperty { get; set; }
        public void ExecuteMethod(object sender)
        {
            ButtonClickMsg = "ExecuteMethod Called - Counter # " + CallCounter;
        }

        public Func<object, bool> CanExecuteProperty { get; set; }
        public bool CanExecuteMethod(object sender)
        {
            return CanExecuteFlag;
        }

        public event Action ActionNotifyCanExecuteChanged;
        public event EventHandler EventHandlerNotifyCanExecuteChanged;

        private Action _propertyDelegateNotifyCanExecuteChanged;
        public Action PropertyDelegateNotifyCanExecuteChanged
        {
            get { return _propertyDelegateNotifyCanExecuteChanged; }
            set { _propertyDelegateNotifyCanExecuteChanged = value; }
        }


        [ViewTarget("Command.Execute")]
        private void ButtonAutoWired(object sender)
        {
            ButtonClickMsg = "ButtonAutoWired ExecuteMethod Called - Counter # " + CallCounter;
        }

        private bool _bttonAutoWireProp = true;

        [ViewXNameAlias("ButtonAutoWired","Command.CanExecute")]
        public bool BttonAutoWireProp
        {
            get 
            { 
                return _bttonAutoWireProp; 
            }
            set { _bttonAutoWireProp = value; }
        }

    }
}
